package com.dferreira.languagesteach.utils;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;

import com.dferreira.languagesteach.meta_data.InteractionUtilities;

public class AudioDelegator implements OnCompletionListener {

    private final Handler mHandler;
    private final Runnable completionDelegator;
    private final MediaPlayer mp;
    private Boolean scheduleMoreEvents;
    private InteractionUtilities unitInteraction;
    private final Integer waitBeforeNextSlide;

    /**
     * Will pass the required information to to reproduce an audio
     *
     * @param mHandler            responsible to schedule more plays
     * @param completionDelegator who will be called after a sound be reproduced
     * @param waitBeforeNextSlide time between end of sound and next completionDelegator call
     */
    public AudioDelegator(Handler mHandler, Runnable completionDelegator, Integer waitBeforeNextSlide) {

        this.mHandler = mHandler;
        this.completionDelegator = completionDelegator;
        this.waitBeforeNextSlide = waitBeforeNextSlide;

        mp = new MediaPlayer();
        mp.setOnCompletionListener(this);
    }

    /**
     * Reproduce an audio file
     *
     * @param path path to the file to reproduce
     * @return false   -> some error happened
     * true    -> everything was fine
     */
    public boolean playPath(String path) {
        try {
            mp.reset();
            mp.setDataSource(path);
            mp.prepare();
            mp.start();
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * Will re-play the las asset
     */
    public void replayLastAsset() {
        mp.seekTo(0);
        if (!mp.isPlaying()) {
            mp.start();
        }
    }

    /**
     * Will stop the reproduction of an audio
     */
    public void stopAudio() {
        if (mp.isPlaying()) {
            mp.seekTo(mp.getDuration());
        }
    }


    /**
     * @param mp the MediaPlayer that reached the end of the file
     */
    @Override
    public void onCompletion(MediaPlayer mp) {

        try {
            unitInteraction.getLookHumanInteraction().acquire();
            unitInteraction.setScheduleTimestamp();
            if (scheduleMoreEvents) {
                mHandler.postDelayed(completionDelegator, waitBeforeNextSlide);
            }
            unitInteraction.getLookHumanInteraction().release();
        } catch (InterruptedException ignored) {
        }
    }

    /**
     * @param scheduleMoreEvents Indicate if is to schedule more events
     */
    public void setScheduleMoreEvents(Boolean scheduleMoreEvents) {
        this.scheduleMoreEvents = scheduleMoreEvents;
    }

    /**
     * @param unitInteraction interface that block the user interface momentarily
     */
    public void setUnitInteraction(InteractionUtilities unitInteraction) {
        this.unitInteraction = unitInteraction;
    }

}
